package com.ruoyi.service.impl;

import com.ruoyi.domain.CategoryOne;
import com.ruoyi.domain.CategoryThree;
import com.ruoyi.domain.CategoryTwo;
import com.ruoyi.mapper.CategoryOneMapper;
import com.ruoyi.mapper.CategoryThreeMapper;
import com.ruoyi.mapper.CategoryTwoMapper;
import com.ruoyi.service.ICategoryOneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class CategoryOneServiceImpl implements ICategoryOneService {
    @Autowired
    private CategoryOneMapper categoryOneMapper;

    @Autowired
    private CategoryTwoMapper categoryTwoMapper;

    @Autowired
    private CategoryThreeMapper categoryThreeMapper;

    @Override
    public List<Map<String, Object>> selectListCascader() {
        List<CategoryOne> oneList = categoryOneMapper.selectList();
        List<CategoryTwo> twoList = categoryTwoMapper.selectList();
        List<CategoryThree> thrList = categoryThreeMapper.selectList();

        List<Map<String, Object>> data = oneList.stream().map(one -> {
            Map<String, Object> map = new HashMap<>();
            map.put("value", one.getConeId());
            map.put("label", one.getConeName());
            // 根据一级 id 为外键 查询 二级集合
            List<Map<String, Object>> twoDataList = twoList.stream()
                    .filter(two -> two.getConeId().equals(one.getConeId()))
                    .map(two -> {
                        Map<String, Object> twoMap = new HashMap<>();
                        twoMap.put("value", two.getCtwoId());
                        twoMap.put("label", two.getCtwoName());
                        // 根据二级 id 为外键 查询 三级集合
                        List<Map<String, Object>> thrDataList = thrList.stream()
                                .filter(thr -> two.getCtwoId().equals(thr.getCtwoId()))
                                .map(thr -> {
                                    Map<String, Object> thrMap = new HashMap<>();
                                    thrMap.put("value", thr.getCthrId());
                                    thrMap.put("label", thr.getCthrName());
                                    return thrMap;
                                }).collect(Collectors.toList());
                        twoMap.put("children", thrDataList);
                        return twoMap;
                    }).collect(Collectors.toList());
            map.put("children", twoDataList);
            return map;
        }).collect(Collectors.toList());

        return data;
    }

    @Override
    public List<CategoryOne> list() {
        return categoryOneMapper.selectList();
    }

    @Override
    public CategoryOne getById(Long id) {
        return categoryOneMapper.selectByPrimaryKey(id);
    }

    @Override
    public int save(CategoryOne categoryOne) {
        return categoryOneMapper.insertSelective(categoryOne);
    }

    @Override
    public int updateById(CategoryOne categoryOne) {
        return categoryOneMapper.updateByPrimaryKeySelective(categoryOne);
    }
}
